﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cjwygl
{
    public partial class FrmOpMgr : Form
    {
        public FrmOpMgr()
        {
            InitializeComponent();
        }

        private void FrmOpMgr_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void button_addop_Click(object sender, EventArgs e)
        {
            
            //int iId = -1;
            
            //int.TryParse(dataGridView1.SelectedRows[0].Cells["ID"].ToString(), out iId);
            FrmOpUpdate frmupdate = new FrmOpUpdate();
            frmupdate.ShowDialog();
            
        }
        private void LoadData()
        {
            this.view_ADMIN_ROLETableAdapter.Fill(this.cjwyglDataSet_op.View_ADMIN_ROLE);
        }
        private void button_modify_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }
            else
            {
                int id = -1;
                int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);
                FrmOpUpdate frmupdate = new FrmOpUpdate(id);
                frmupdate.ShowDialog();

            }
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            DialogResult dr= MessageBox.Show("确定要删除吗？", "删除该条记录", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }
            else
            {
                if (dataGridView1.SelectedRows[0].Cells[1].Value.ToString() == userinfo.username)
                {
                    MessageBox.Show("登录状态下,不能删除自己");
                    return;
                }
                int id = -1;
                int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);
                if (id == -1)
                {
                    MessageBox.Show("系统错误，未识别的行");
                }
                else
                {
                    using (DataBase db = new DataBase())
                    {
                        string sql = "delete from CJ_ADMIN where ID=@ID";
                        db.AddParameter("ID", id);
                        int ret=db.ExecuteNonQuery(sql);
                        MessageBox.Show("已删除" + ret + "行");
                        LoadData();
                    }
                }

            }
        }

        private void button_refresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void button_search_Click(object sender, EventArgs e)
        {
            string[] fields = {"USERNAME","NICKNAME","ROLE_NAME","PERMISSIONS" };

            string sql = "SELECT a.ID,USERNAME,NICKNAME,ROLE_NAME,PERMISSIONS "+
                "FROM CJ_ADMIN a,CJ_ROLE b WHERE a.ROLE_ID=b.ID AND ( 1=2 ";
            string key = textBox_search.Text.Trim();
            foreach (string field in fields)
            {
                sql += "OR " + field + " LIKE '%" + key + "%' ";
            }
            sql += ")";
            using (DataBase db = new DataBase())
            {
                DataTable dt = db.ExecuteDataTable(sql);
                this.viewADMINROLEBindingSource.DataSource = dt;
                LoadData();
            }
        }
    }
}
